package com.atguigu.controller;

import com.atguigu.entity.Admin;
import com.atguigu.entity.Role;
import com.atguigu.result.Result;
import com.atguigu.service.AdminRoleService;
import com.atguigu.service.AdminService;
import com.atguigu.util.QiniuUtil;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("/admin")
public class AdminController extends BaseController {

    @DubboReference
    private AdminService adminService;
    @DubboReference
    private AdminRoleService adminRoleService;
    @Autowired
    private PasswordEncoder passwordEncoder;

    @RequestMapping
    public String getAll(Map map, HttpServletRequest request) {
        Map<String, Object> filters = getFilters(request);
        PageInfo<Admin> page = adminService.findPage(filters);
        map.put("page", page);
        map.put("filters", filters);
        return "admin/index";
    }
    @RequestMapping("/create")
    public String create() {
        return "admin/create";
    }

    @RequestMapping("/save")
    public String insert(Admin admin) {
        admin.setPassword(passwordEncoder.encode(admin.getPassword()));
        adminService.insert(admin);
        return "common/success";
    }

    @RequestMapping("/edit/{id}")
    public String edit(@PathVariable Long id,Map map) {
        Admin admin = adminService.getById(id);
        map.put("admin", admin);
        return "admin/edit";
    }



    @RequestMapping("/update")
    public String update(Admin admin) {
        adminService.update(admin);
        return "common/success";
    }

    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable Long id) {
        adminService.delete(id);
        return "redirect:/admin";
    }
    @RequestMapping("/uploadShow/{adminId}")
    public String uploadShow(@PathVariable Long adminId,Map map) throws IOException {
        map.put("adminId", adminId);
        return "admin/upload";
    }
    @RequestMapping("/upload")
    public String upload(Long adminId,@RequestParam("file")MultipartFile[] files) throws IOException {
        for (MultipartFile file : files) {
            String filename = UUID.randomUUID().toString();
            QiniuUtil.upload2Qiniu(file.getBytes(),filename);
            String url = "http://rod3a7eig.hn-bkt.clouddn.com/" + filename;
            adminService.updateUrlById(adminId,url);
        }
        return "common/success";
    }
    @RequestMapping("/assignShow/{adminId}")
    public String assignShow(@PathVariable Long adminId,Map map){
        map.put("adminId", adminId);
        Map<String, List<Role>> roleByAdminId = adminRoleService.findRoleByAdminId(adminId);
        map.putAll(roleByAdminId);
        return "admin/assignShow";
    }
    @RequestMapping("/assignRole")
    public String assignRole(Long adminId,String[] roleIds) {
        adminRoleService.insertAdminRole(adminId,roleIds);
        return "common/success";
    }

}
